package com.inst.jsoup_html_demo.fragment.caipuFragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.inst.jsoup_html_demo.MyApplication;
import com.inst.jsoup_html_demo.R;
import com.inst.jsoup_html_demo.activity.ZhuanTiActivity;
import com.inst.jsoup_html_demo.adapter.HomeAdapter;
import com.inst.jsoup_html_demo.bean.HomeTab;
import com.inst.jsoup_html_demo.network.BaseUrl;
import com.inst.jsoup_html_demo.utils.IntentUtils;
import com.inst.jsoup_html_demo.view.MyViewFlipper;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.bgabanner.BGABanner;


/**
 *
 */

public class CaiPuHomeFragment extends Fragment {
    @BindView(R.id.iv_search_icon)
    ImageView ivSearchIcon;
    @BindView(R.id.ll_caidanfenlei)
    LinearLayout llCaidanfenlei;
    @BindView(R.id.ll_shicaidaquan)
    LinearLayout llShicaidaquan;
    @BindView(R.id.ll_zhuanti)
    LinearLayout llZhuanti;
    @BindView(R.id.ll_caidan)
    LinearLayout llCaidan;
    @BindView(R.id.ll_zhencang)
    LinearLayout llZhencang;
    @BindView(R.id.ll_yizhouremen)
    LinearLayout llYizhouremen;
    @BindView(R.id.ll_renqicaiyao)
    LinearLayout llRenqicaiyao;
    @BindView(R.id.ll_jiachangcai)
    LinearLayout llJiachangcai;
    @BindView(R.id.ll_shiliaoyangsheng)
    LinearLayout llShiliaoyangsheng;
    @BindView(R.id.ll_fulishe)
    LinearLayout llFulishe;
    @BindView(R.id.flipper_meitiantoupan)
    MyViewFlipper mMyViewFlipper;

    @BindView(R.id.banner_splash_pager)
    BGABanner mBGABanner;
    RelativeLayout resultView;
    ArrayList<View> arrayList = new ArrayList<View>();
    Unbinder unbinder;
    private String mFrom;
    private ArrayList<HomeTab> zhuanTiArrayList;
    private HomeAdapter mAdapter;


    public static CaiPuHomeFragment newInstance(String from) {
        CaiPuHomeFragment fragment = new CaiPuHomeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("from", from);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mFrom = getArguments().getString("from");
            Log.e("CaiPuHomeFragment", "onCreate:"+mFrom);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_caipu_tab_home, null);
        unbinder = ButterKnife.bind(this, view);
        Log.e("CaiPuHomeFragment", "onCreateView:");
        return view;
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 200:
                    initData();
                    break;
            }
        }
    };


    private void initData() {
        for (int i = 0; i < 3; i++) {
            View view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.flipper_view, null);
            TextView viewText = (TextView) view.findViewById(R.id.tv_flipper_title);
            viewText.setText("每日推荐" + i);
            mMyViewFlipper.addView(view);
        }
        //给轮播图填充图片
        if (zhuanTiArrayList != null && zhuanTiArrayList.size() > 0) {
            mBGABanner.setData(Arrays.asList(zhuanTiArrayList.get(0).getImageUrl(), zhuanTiArrayList.get(1).getImageUrl(), zhuanTiArrayList.get(2).getImageUrl(), zhuanTiArrayList.get(3).getImageUrl(), zhuanTiArrayList.get(4).getImageUrl()), Arrays.asList("", "", "", "", ""));
            //
            mBGABanner.setAdapter(new BGABanner.Adapter<ImageView, String>() {
                @Override
                public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                    Glide.with(getActivity())
                            .load(model)
                            .placeholder(R.drawable.menu_bg)
                            .error(R.drawable.menu_bg)
                            .dontAnimate()
                            .into(itemView);
                }
            });
            //图片item点击事件,BGABanner 里已经帮处理了防止重复点击事件
            mBGABanner.setDelegate(new BGABanner.Delegate<ImageView, String>() {
                @Override
                public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {
                    IntentUtils.toActivity(getActivity(), ZhuanTiActivity.class);
                    Toast.makeText(banner.getContext(), "点击了" + position, Toast.LENGTH_SHORT).show();
                }
            });
        }

        mMyViewFlipper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("mMyViewFlipper", mMyViewFlipper.getDisplayedChild() + "");
            }
        });


    }

    /***
     * 请求网络数据
     */
    private void getData(final String url) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //从一个URL加载一个Document对象。
                    zhuanTiArrayList = new ArrayList<HomeTab>();
                    Document doc = Jsoup.connect(BaseUrl.homeUrl).get();
                    Element content = doc.getElementById("home_index_slider");
                    Elements links_ul = content.getElementsByTag("ul");
                    Elements links = links_ul.get(0).getElementsByTag("a");
                    for (int i = 1; i < links.size(); i++) {
                        HomeTab homeTab = new HomeTab();
                        String linkTitle = links.get(i).attr("title");
                        Elements linksrc = links.get(i).getElementsByTag("img");
                        String linkSrc = linksrc.get(0).attr("src");
                        String linkHref = links.get(i).attr("href");
                        homeTab.setImageUrl(linkSrc);
                        homeTab.setTitle(linkTitle);
                        homeTab.setZhuanTiUrl(linkHref);
                        Log.e("mytitle", homeTab.getTitle());
                        Log.e("mysrc", homeTab.getImageUrl());
                        Log.e("myhref", homeTab.getZhuanTiUrl());
                        zhuanTiArrayList.add(homeTab);
                    }
                    Message message = new Message();
                    message.what = 200;
                    mHandler.sendMessage(message);
                } catch (Exception e) {
                    Log.e("mytag", e.toString());
                }
            }
        }).start();
    }

    @Override
    public void onResume() {
        super.onResume();
        getData(BaseUrl.homeUrl);
        Log.e("CaiPuHomeFragment", "onResume:");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        Log.e("CaiPuHomeFragment", "onDestroyView:");
    }

}
